package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.DailyCatalog;
import cn.wolfcode.trip.base.mapper.DailyCatalogMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IDailyCatalogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DailyCatalogServiceImpl implements IDailyCatalogService {
    @Autowired
    private DailyCatalogMapper dailyCatalogMapper;

    public List<DailyCatalog> listCatalogByDailyId(Long dailyId) {
        return dailyCatalogMapper.selectByDailyId(dailyId);
    }

    public void saveOrUpdate(DailyCatalog entity) {
        //自动生成序列
        if (entity.getSequence() == null) {
            //查询当前所属攻略序列的最大值
            Integer sequence = dailyCatalogMapper.selectMaxSequence(entity.getDailyId());
            entity.setSequence(sequence == null ? 1 : sequence + 1);
        }

        if (entity.getId() == null) {
            dailyCatalogMapper.insert(entity);
        } else {
            dailyCatalogMapper.updateByPrimaryKey(entity);
        }
    }

    public PageInfo<DailyCatalog> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<DailyCatalog> list = dailyCatalogMapper.selectForList(qo);
        return new PageInfo<>(list);
    }
}
